%==========================================================================
% Title : Phase Correction Function
% Version : 3.2
% Modified : 03 May, 2013
% Author : Sungjin Kim
% 
% ===========
%  Arguments 
% ===========
% 1. filename
% 2. zero order shift angle
% 3. first order shift factor
% 4. first order shift center offset
% 5. channel
% 6. selection of real or imaginary spectrum
% 7. selection of windows to plot a spectrum whether on reference or current work space.
% 8. event structure for future function.
%==========================================================================

function [headerInfo, output] = adjustphase_f(filename, zero_order_shift_angle, first_order_shift_factor, ...
                                   center_offset, channel, real_imag_select, window_sel, u_events)
    %% Parameters %%
    handles=guidata(gcbo);                     % obtain parameter from caller
    frequencyTx = handles.TxFreq;              % frequency of transmitter (MHz) 
    bandwidth = handles.bandwidth;             % bandwidth (Hz)
    shiftoffset = 5.22;                        % shift offset (ppm)
    shift_direction = 1;                       % for left, input 0, or for right, input 1.

    %% reading data %%
    [realFidData, imagFidData, headerInfo] = readfid(filename, channel);
    len_data = length(realFidData);                 % The number of points in data 
    complex_FidData=realFidData+1i*imagFidData;     % complex type data
    
    %% Apodization %%
    if u_events{1}(1) == 1
        if u_events{1}(2) == 0              % Lorentzian 
            complex_FidData=apodizeFID(complex_FidData, u_events{1}(3), bandwidth, u_events);
        else                                % Gaussian
            complex_FidData=apodizeFID(complex_FidData, u_events{1}(3), bandwidth, u_events);
        end
    end
   

    %% Debugging - inform the parameters %%
%     fprintf('\n======================================================= \n');
%     fprintf('\tThe frequency of transmitter is %d MHz \n', frequencyTx);
%     fprintf('\tThe bandwidth is %d Hz\n', bandwidth);
%     fprintf('\tThe value of shift is %.2f ppm \n', shiftoffset);
%     fprintf('\tThis data is from the channel %d \n', channel);
%     fprintf('\tThe number of points is %d in data \n', len_data);
%     fprintf('======================================================= \n');

    %% fast fourier tansform %%
    complex_freDomain_orgin = fft(complex_FidData);

    %% shift the data whether right or left %%
    num_shift = uint32(frequencyTx*shiftoffset*bandwidth/len_data);                             % The unit is 'Hz' 
    complex_freDomain_orgin = datashift(complex_freDomain_orgin, num_shift, shift_direction);   % for left, input 0, or for right, input 1.

    %% Phase shift %%
    [complex_PC_freDomain, complex_PC_FidData]= phaseShift(complex_freDomain_orgin, zero_order_shift_angle, first_order_shift_factor, center_offset, num_shift, shift_direction);

    %% ploting %%    
    plotspectra(complex_PC_freDomain, frequencyTx, bandwidth, 1, real_imag_select, window_sel, handles);
    output = complex_PC_FidData;

    return;
end
